package cc.bookcloud.tool.ocr.common;
/*==========================================================================
 * Copyright (C) Wit2Cloud Co.,Ltd
 * All Rights Reserved.
 * Created By 开源学社
 ==========================================================================*/

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.dialect.impl.Sqlite3Dialect;
import com.alibaba.fastjson.JSON;
import org.sqlite.SQLiteConfig;
import org.sqlite.SQLiteDataSource;

import java.sql.*;
import java.util.List;

/**
 * @author John Goo
 * @version 1.0
 * @ClassName: Sqlite3Demo
 * @Desc: TODO
 * @history v1.0
 */
public class Sqlite3Demo {



    /**
     * Connect to a sample database
     */
    public static void connect() {
        //Connection conn = null;
        try {
            // db parameters
            String url = "jdbc:sqlite:G:\\2022-code\\release-assist-tool\\src\\main\\resources\\x_release.db";
            // create a connection to the database
            //conn = DriverManager.getConnection(url);

            try {
                Class.forName("org.sqlite.JDBC");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            SQLiteConfig conf = new SQLiteConfig();
            conf.setSharedCache(true);
            conf.setSynchronous(SQLiteConfig.SynchronousMode.OFF);
            conf.createConnection(url);
            // conf.setJournalMode(JournalMode.MEMORY);
            SQLiteDataSource ds = new SQLiteDataSource(conf);
            ds.setUrl(url);
            
            System.out.printf("====conn:"+ds.getConnection());
            System.out.println("Connection to SQLite has been established.");
            Statement stmt = ds.getConnection().createStatement();
            String sql = "SELECT * FROM employee";
            List<Entity> entityList = Db.use(ds, new Sqlite3Dialect()).query(sql);

            System.out.println("===db query======"+ JSON.toJSONString(entityList));
            ResultSet rs = stmt.executeQuery(sql);

            // loop through the result set
            while (rs.next()) {
                //empid  deptid  salary
                System.out.println(rs.getInt("empid") + "\t" + rs.getInt("deptid") + "\t" + rs.getDouble("salary"));
            }
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        } finally {

        }
    }


    public static void main(String[] args) {
        connect();

    }
}
